package org.airline.action;

import java.io.ByteArrayInputStream;
import java.util.ArrayList;

import javax.annotation.Resource;

import org.airline.common.IModuleName;
import org.airline.entity.FlightArrange;
import org.airline.entity.User;
import org.airline.service.UserService;
import org.airline.utils.GenerateCode;
import org.apache.struts2.ServletActionContext;
import org.springframework.stereotype.Component;

@Component("AjaxAction")
@SuppressWarnings("all")
public class AjaxAction extends SuperAction {

	private User user;
	private UserService userService;
	private boolean flag;
	private ByteArrayInputStream image;
	private String random;
	private String forwardURL;
	private IModuleName iModuleName;


	// 检测用户名是否已存在
	public String checkUserName() throws Exception {
		try {
			if (this.userService.checkUserName(user.getUserName())) {
				this.flag = true;
			} else {
				this.flag = false;
			}
		} catch (Exception e) {
			super.logFailureString(iModuleName.GET, iModuleName.GET, "检测用户名失败",
					e);
			e.printStackTrace();
		}
		super.logSuccessString(iModuleName.GET, iModuleName.GET, "检测用户名成功");
		return "check";
	}

	// 产生验证码
	public String generateCode() throws Exception {
		try {
			GenerateCode generateCode = GenerateCode.Instance();
			this.image = generateCode.getImage();
			ServletActionContext.getRequest().getSession().setAttribute(
					"random", generateCode.getCodeStr());
		} catch (Exception e) {
			super.logFailureString(iModuleName.ADD, iModuleName.ADD, "验证码生成失败",
					e);
			e.printStackTrace();
		}
		super.logSuccessString(iModuleName.ADD, iModuleName.ADD, "验证码生成成功");
		return SUCCESS;
	}

	// 比较验证码
	public String checkValidateCode() throws Exception {
		try {
			String randomCode = (String) ServletActionContext.getRequest()
					.getSession().getAttribute("random");
			if (random.equalsIgnoreCase(randomCode)) {
				this.flag = true;
			} else {
				this.flag = false;
			}
		} catch (Exception e) {
			super.logFailureString(iModuleName.COMPARE, iModuleName.COMPARE,
					"验证码比较失败", e);
			e.printStackTrace();
		}
		super.logSuccessString(iModuleName.COMPARE, iModuleName.COMPARE,
				"验证码比较成功");
		return "check";
	}

	// 检测邮箱是否已被注册
	public String checkEamil() throws Exception {
		try {
			if (this.userService.checkEamil(user.getEmail())) {
				this.flag = true;
			} else {
				this.flag = false;
			}
		} catch (Exception e) {
			super.logFailureString(iModuleName.GET, iModuleName.GET, "检测邮箱失败",
					e);
			e.printStackTrace();
		}
		super.logSuccessString(iModuleName.GET, iModuleName.GET, "检测邮箱成功");
		return "check";
	}

	public User getUser() {
		return user;
	}

	public void setUser(User user) {
		this.user = user;
	}

	public UserService getUserService() {
		return userService;
	}

	@Resource(name = "UserService")
	public void setUserService(UserService userService) {
		this.userService = userService;
	}

	public boolean isFlag() {
		return flag;
	}

	public void setFlag(boolean flag) {
		this.flag = flag;
	}

	public ByteArrayInputStream getImage() {
		return image;
	}

	public void setImage(ByteArrayInputStream image) {
		this.image = image;
	}

	public String getRandom() {
		return random;
	}

	public void setRandom(String random) {
		this.random = random;
	}

	public String getForwardURL() {
		return forwardURL;
	}

	public void setForwardURL(String forwardURL) {
		this.forwardURL = forwardURL;
	}

	public IModuleName getiModuleName() {
		return iModuleName;
	}

	public void setiModuleName(IModuleName iModuleName) {
		this.iModuleName = iModuleName;
	}
}
